package com.file.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Customer;
import com.entity.CustomerMeter;
import com.entity.CustomerMeterPK;
import com.entity.Meter;
import com.file.entity.FileCustomerMeter;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.SysdicDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: CustomerMeterService.java
 * @Package com.file.service
 * @author 孙梦楠
 * @date 2017年7月31日 上午10:31:41
 * @version V1.0.0
 */
@Service
public class CustomerMeterService {

	private static final Logger log = LoggerFactory.getLogger(CustomerMeterService.class);

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private MeasuringpointDao measuringpointdao;
	
	@Autowired
	private SysdicService sysdicservice;

	// 条件列表
	@Transactional
	public JsonResult<List<FileCustomerMeter>> ListcustomermeterByConditions(String customeraccount,
			String customername, String customeraddress, String opeorgcookie,String languagecookie,String opetypecookie) {

		if (Utils.isNull(customeraccount) && Utils.isNull(customername) && Utils.isNull(customeraddress)) {

			List<CustomerMeter> customermeters = customermeterdao.findByOrg(Integer.valueOf(opeorgcookie),opetypecookie);

			return new JsonResult<List<FileCustomerMeter>>(271, "展示关联关系列表", dofilecustomermeter(customermeters,languagecookie));

		} else {

			List<CustomerMeter> customermeters = customermeterdao.findByCustomerConditions(
					Integer.valueOf(opeorgcookie), customeraccount, customername, customeraddress,opetypecookie);

			return new JsonResult<List<FileCustomerMeter>>(277, "展示条件关联关系列表", dofilecustomermeter(customermeters,languagecookie));

		}

	}

	// 增加关联
	@Transactional
	public JsonResult<CustomerMeter> addcustomermeter(Integer customerid, Integer meterid) {

		if (customerid == null || meterid == null) {

			return new JsonResult<CustomerMeter>(274, "增加用户和表关联关系失败..数值不能为空", null);
		} else {

			// 设置双主键
			CustomerMeterPK pk = new CustomerMeterPK();
			pk.setCustomerid(customerid);
			pk.setMeterid(meterid);
			CustomerMeter customermeter = new CustomerMeter();
			customermeter.setPk(pk);

			// 更改表状态为 已安装
			meterdao.updateMeter("2", meterid);
			// 更新用户状态为 已连接
			customerdao.updateCustomer("2", customerid);
			//更新户号为表出厂编号
			customerdao.updateCustomeraccount(meterdao.findOne(meterid).getMetercode(), customerid);
			// 保存关系
			customermeterdao.save(customermeter);

			return new JsonResult<CustomerMeter>(275, "增加用户和表关联关系成功", customermeter);
		}

	}

	// 删除关联关系
	@Transactional
	public JsonResult<CustomerMeter> deletecustomermeter(Integer customerid,String opeorgcookie,String customertype) {

		if (customermeterdao.findByCustomerid2(customerid) != null) {
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();

			if (customerid != null && meterid != null ) {
				// 更改表状态为 待用
				meterdao.updateMeter("1", meterid);
				// 更新用户状态为 未连接
				customerdao.updateCustomer("1", customerid);
				//更新用户户号为新生成的户号
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				String customeraccount = sdf.format(new Date());
				if (customerdao.findByCustomeraccountLikeDESC(customeraccount, Integer.valueOf(opeorgcookie), customertype)
						.size() == 0) {
					customeraccount = sdf.format(new Date()) + "0001";
				} else {
					String s = customerdao
							.findByCustomeraccountLikeDESC(customeraccount, Integer.valueOf(opeorgcookie), customertype)
							.get(0).getCustomeraccount();
					s = s.substring(s.length() - 4);
					Integer i = Integer.valueOf(s) + 1;
					s = String.format("%04d", i);
					customeraccount = sdf.format(new Date()) + s;
				}
				customerdao.updateCustomeraccount(customeraccount, customerid);
				
				// 保存关系
				CustomerMeterPK pk = new CustomerMeterPK();
				pk.setCustomerid(customerid);
				pk.setMeterid(meterid);
				CustomerMeter customermeter = new CustomerMeter();
				customermeter.setPk(pk);
				// 删除
				customermeterdao.delete(customermeter);

				return new JsonResult<CustomerMeter>(276, "删除用户和表关联关系成功", null);
			} else {
				return new JsonResult<CustomerMeter>(278, "删除用户和表关联关系失败..ID为空", null);
			}
		}
		return new JsonResult<CustomerMeter>(281, "删除用户和表关联关系失败..用户未连接表", null);
	}

	// 查询单个（修改界面）
	@Transactional
	public JsonResult<CustomerMeter> updateviewcustomermeter(Integer customerid, Integer meterid) {

		if (customerid != null && meterid != null) {
			CustomerMeter customermeter = customermeterdao.findByCustomeridAndMeterid(customerid, meterid);

			return new JsonResult<CustomerMeter>(277, "查单个关联关系", customermeter);
		} else {

			return new JsonResult<CustomerMeter>(279, "查单个关联关系失败..ID为空", null);
		}
	}

	// 组装前端类
	public List<FileCustomerMeter> dofilecustomermeter(List<CustomerMeter> customermeters,String languagecookie) {

		List<FileCustomerMeter> filecustomermeters = new ArrayList<FileCustomerMeter>();

		for (int i = 0; i < customermeters.size(); i++) {
			FileCustomerMeter filecustomermeter = new FileCustomerMeter();

			// 转换为object类
			Object object = customermeters.get(i);
			// 强行转换为PK
			CustomerMeterPK pk = (CustomerMeterPK) object;

			Integer meterid1 = pk.getMeterid();
			Integer customerid1 = pk.getCustomerid();

			Meter meter = meterdao.findOne(meterid1);
			Customer customer = customerdao.findOne(customerid1);
			// 表数据
			filecustomermeter.setMeterid(meterid1);
			filecustomermeter.setMetercode(meter.getMetercode());
			filecustomermeter.setMeterassetcode(meter.getMeterassetcode());
			filecustomermeter.setMetertype(meter.getMetertype());
			filecustomermeter.setMetertypename(sysdicservice.finddisplay("0005", meter.getMetertype(),languagecookie));
			filecustomermeter.setMeteraddress(meter.getMeteraddress());
			filecustomermeter.setMeterprotocol(meter.getMeterprotocol());
			filecustomermeter.setMeterprotocolname(sysdicservice.finddisplay("0011", meter.getMeterprotocol(),languagecookie));
			filecustomermeter.setMetermade(meter.getMetermade());
			filecustomermeter.setMetermadename(sysdicservice.finddisplay("0008", meter.getMetermade(),languagecookie));
			filecustomermeter.setMeterorg(meter.getMeterorg());
			filecustomermeter.setMeterorgname(sysdicservice.finddisplay("0001", meter.getMeterorg().toString(),languagecookie));
			filecustomermeter.setMeterstatus(meter.getMeterstatus());
			filecustomermeter.setMeterstatusname(sysdicservice.finddisplay("0009", meter.getMeterstatus(),languagecookie));
			filecustomermeter.setMeterpassword(meter.getMeterpassword());
			// 用户数据
			filecustomermeter.setCustomerid(customerid1);
			filecustomermeter.setCustomeraccount(customer.getCustomeraccount());
			filecustomermeter.setCustomername(customer.getCustomername());
			filecustomermeter.setCustomeraddress(customer.getCustomeraddress());
			// 采集终端数据
			filecustomermeter.setTerminalid(measuringpointdao.findByMeterid(meterid1).getTerminalid());

			filecustomermeters.add(filecustomermeter);
		}
		return filecustomermeters;

	}


}
